package com.example.studentsystem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.studentsystem.bean.UserBean;
import com.example.studentsystem.dao.MyDao;

public class ZhuCe extends AppCompatActivity implements View.OnClickListener {
    EditText id;//注册的用户名
    EditText passward2;//注册的密码
    EditText passward3;//确认密码
    Button btn;//注册按钮
    EditText name,banji;
    RadioGroup rg;
    String sex;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce);
        id = findViewById(R.id.id);
        passward2 = findViewById(R.id.passward2);
        passward3 = findViewById(R.id.passward3);
        btn = findViewById(R.id.btn);
        name=findViewById(R.id.name);
        banji=findViewById(R.id.banji);
        rg = findViewById(R.id.rg);
        btn.setOnClickListener(this);
        setTitle("注册页面");
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton=findViewById(i);
                 sex =radioButton.getText().toString();
            }
        });
    }
    @Override
    public void onClick(View view) {
        String username = id.getText().toString();
        String pass2 =  passward2.getText().toString();
        String pass3 = passward3.getText().toString();
        if(username.equals("")  || pass2.equals("")  || pass3.equals("") ) {//如果用户名或密码或确认密码为空，则打印“用户名或密码不能为空”
            Toast.makeText(ZhuCe.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
        }
        else {
            if(!pass2.equals(pass3)){//如果密码和确认密码不一致，则打印“两次密码不一致，请重新输入”
                Toast.makeText(this,"两次密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
            }
            else {
                Intent intent = new Intent();
                intent.putExtra("username",username);//存放注册得到的用户名
                setResult(RESULT_OK,intent);
                intent.putExtra("passward",pass2);//存放注册得到的密码
                setResult(RESULT_OK,intent);
                final UserBean userBean = new UserBean();
                userBean.setName(name.getText().toString());
                userBean.setId(Integer.parseInt(id.getText().toString()));
                userBean.setBanji(banji.getText().toString());
                userBean.setSex(sex);
                MyDao myDao = new MyDao(this);
                myDao.insert(userBean);
                intent.putExtra("a",userBean);
                intent.setClass(ZhuCe.this,MainActivity.class);
                startActivity(intent);
            }
        }
    }
}